//
//  PCCommentCell.m
//  PhotoCircle
//
//  Created by GL on 15/12/20.
//  Copyright © 2015年 wujungao. All rights reserved.
//

#import "PCCommentCell.h"
#import "PCcommentModel.h"
#import "UIImageView+WebCache.h"
#define SCREEN_WIDTH    [UIScreen mainScreen].bounds.size.width

@interface PCCommentCell ()
{
    PCCommentModel *_cellModel;
}
@property (weak, nonatomic) IBOutlet UIImageView *userImageView;
@property (weak, nonatomic) IBOutlet UILabel *userName;
@property (weak, nonatomic) IBOutlet UILabel *timeLable;
@property (weak, nonatomic) IBOutlet UILabel *userComment;
@property (weak, nonatomic) IBOutlet UIButton *likeButton;

@property (weak, nonatomic) IBOutlet UIImageView *cImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cImageHeightConstraint;

@end

@implementation PCCommentCell

-(void)setFrame:(CGRect)frame
{
    frame.size.width = SCREEN_WIDTH;
    [super setFrame:frame];
}

- (void)awakeFromNib {
    // Initialization code
    _likeButton.layer.cornerRadius = _likeButton.frame.size.height/2.0;
    _likeButton.layer.masksToBounds = YES;
    //设置边框及边框颜色
    _likeButton.layer.borderWidth = 0.5;
    _likeButton.layer.borderColor =[PCColorC4 CGColor];
    
    [_likeButton setImage:[UIImage imageNamed:@"like_select"] forState:UIControlStateSelected];
    [_likeButton setImage:[UIImage imageNamed:@"like_no_select"] forState:UIControlStateNormal];
    _likeButton.titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;
    
}

-(void)tapImage:(UITapGestureRecognizer *)tap
{
    // 点击缩略图
    if ([self.delegate respondsToSelector:@selector(thumbTapWithCellModel:)]) {
        [self.delegate thumbTapWithCellModel:_cellModel];
    }
}

-(void)configCellWithModel:(PCCommentModel *)model{
    
    _cellModel = model;
    //self.cImageView.hidden = YES;
    [self.userImageView sd_setImageWithURL:[NSURL URLWithString:model.commentUser.headImgeUrl]];
    self.userName.text = model.commentUser.name;
    self.userComment.text = model.content;
    self.timeLable.text = model.timeStr;
    [self.likeButton setTitle:[NSString stringWithFormat:@"%ld",model.like_count] forState:UIControlStateNormal];
    self.likeButton.selected = model.is_like;
    if (model.is_like) {
        self.likeButton.userInteractionEnabled = NO;
    }else{
        self.likeButton.userInteractionEnabled = YES;
    }
    
    if (_cellModel.thumbImage && [_cellModel.thumbImage length] >0 ) {
        self.cImageHeightConstraint.constant = 80;
        NSURL *imgUrl = [NSURL URLWithString:_cellModel.thumbImage];
        [self.cImageView sd_setImageWithURL:imgUrl placeholderImage:[UIImage imageNamed:@"PCDefaultImage"]];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage:)];
        [self.cImageView addGestureRecognizer:tapGesture];

    }else {
        self.cImageHeightConstraint.constant = 0;
    }
//    [self setNeedsUpdateConstraints];
//    [self updateConstraintsIfNeeded];
    
//    cell.bounds = CGRectMake(0.0f, 0.0f, CGRectGetWidth(tableView.bounds),
    [self setNeedsLayout];
    [self layoutIfNeeded];
}

-(float)cellHeight{
    
    //float cellHeight = CGRectGetMinY(self.userComment.frame) + CGRectGetHeight(self.userComment.frame)+15;
    float cellHeight = CGRectGetMinY(self.cImageView.frame) + CGRectGetHeight(self.cImageView.frame)+15;
    return cellHeight;
}

- (IBAction)likeEvent:(UIButton *)sender {
    // 点赞
    if (self.delegate != nil && [self.delegate respondsToSelector:@selector(likeButtonClickWithCellModel:)]) {
        
        _cellModel.like_count ++;
        [self.likeButton setTitle:[NSString stringWithFormat:@"%ld",_cellModel.like_count] forState:UIControlStateNormal];
        _cellModel.is_like = YES;
        self.likeButton.userInteractionEnabled = NO;
        self.likeButton.selected = YES;
        [self.delegate likeButtonClickWithCellModel:_cellModel];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
